/**
 * 
 */
package nus.iss.ga.ui;

import javax.swing.JFrame;
import javax.swing.SwingUtilities;
import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.Dimension; 
import java.awt.GridLayout; 
import java.awt.event.MouseAdapter; 
import java.awt.event.MouseEvent; 


/**
 * First setContainerInList (pass List as parameter)
 * ArrayList each row  containing packageid, 
 * Assumption: Nearest destination will be delivered first
 * Call IGAController.runGA()
 * 
 * 
 * Constraints:
 * Destination: Destination A, B,C,D,E, E means 5, D is 4, C is 3, B is 2 and A is 1
 * Item Type:  Fragile (means 1), Non-fragile  : Count of package container fragile is 10 and Non fragile is 54
 */

/**
 * @author Fum Kok Siong
 *
 */
public class GAMainWindow extends JPanel{

	private JButton OK;
	private JLabel optimize;
	private boolean DEBUG = false; 
    
	private static final long serialVersionUID = 1L;
	
	public GAMainWindow() {
		//setTitle("GA Screen");
		setSize(900,800);
		
		JPanel panel = new JPanel();
		panel.setLayout(null);
		
		OK = new JButton("OK");
		JCheckBox cb = new JCheckBox("Enabled", true);

		
		OK.setBounds(40,30,80,25);
		OK.addChangeListener(new ChangeListener() {
			public void stateChanged(ChangeEvent e) {
				DefaultButtonModel model = (DefaultButtonModel) OK.getModel();
	            if (model.isEnabled()){
	            	//optimize.setText("Optimized: true");
	            }else{
	            	//optimize.setText("Optimized: false");
	            }

			}
		});
		
		
        cb.addActionListener(new ActionListener() {

            public void actionPerformed(ActionEvent e) {
                if (OK.isEnabled())
                    OK.setEnabled(false);
                else 
                    OK.setEnabled(true);
            }
        });

        cb.setBounds(180, 30, 100, 25);	
        
        String[] columnNames = {"Package Id", "Weight", "Destination", "Type"};
        Object[][] data = {
        		{"1", "50", "South", "Fragile"},
        		{"2", "55", "North", "Solid"},
        		{"3", "50", "South", "Fragile"},
        		{"4", "55", "North", "Solid"},
        		{"5", "50", "South", "Fragile"},
        		{"6", "55", "North", "Solid"},
        		{"7", "50", "South", "Fragile"},
        		{"8", "55", "North", "Solid"},
        		{"9", "50", "South", "Fragile"},
        		{"10", "55", "North", "Solid"}
        };
        
        final JTable table = new JTable(data, columnNames);
        JScrollPane scrollPane = new JScrollPane(table);
               
        table.setPreferredScrollableViewportSize(new Dimension(600, 200));         
        table.setFillsViewportHeight(true);           
        if (DEBUG) {             
        	table.addMouseListener(new MouseAdapter() {                 
        		public void mouseClicked(MouseEvent e) {                     
        			printDebugData(table);                 
        		}             
             });         
        }           
               
        //Add the scroll pane to this panel.         
        add(scrollPane); 

		panel.add(OK);
        panel.add(cb);
        add(panel);
	
		//setLocationRelativeTo(null);
		//setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
		
	}
	
	
    /**      * Create the GUI and show it.  For thread safety,      * this method should be invoked from the      * event-dispatching thread.      */    
	private static void createAndShowGUI() {         
		//Create and set up the window.         
		JFrame frame = new JFrame("Load Fistribution Solver");         
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);           
		//Create and set up the content pane.         
		GAMainWindow newContentPane = new GAMainWindow();         
		newContentPane.setOpaque(true); //content panes must be opaque         
		frame.setContentPane(newContentPane);           
		//Display the window.         
		frame.pack();         
		frame.setVisible(true);     
     } 
	
	



	 private void printDebugData(JTable table) {         
		 int numRows = table.getRowCount();         
		 int numCols = table.getColumnCount();         
		 javax.swing.table.TableModel model = table.getModel();           
		 System.out.println("Value of data: ");         
		 for (int i=0; i < numRows; i++) {             
			 System.out.print("    row " + i + ":");             
			 for (int j=0; j < numCols; j++) {                 
				 System.out.print("  " + model.getValueAt(i, j));             }             
			 System.out.println();         }         
		 System.out.println("--------------------------");     
      }  
	 
	 
	 
	 
	 
	 
	 
	public static void main(String[] args){
		 javax.swing.SwingUtilities.invokeLater(new Runnable() {             
			 public void run() {                 
				 createAndShowGUI();             
		     }         
		  }); 
		
		
	}
}
